// tbkPackage/pages/proList.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        channel: "jd",
        type: "hot",
        value: "1",
        page: 1,
        proList: {},
        leader: 0,
        commissionname: "返利",
        floorstatus: false,
        noshare: true,
        noData: false
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        //获取转发的团长信息
                if (options.leader) {
            that.setData({
                leader: options.leader
            });
        }
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        //页面平台基础信息
                let channel = options.channel == "" || options.channel == undefined ? "jd" : options.channel;
        let type = options.type == "" || options.type == undefined ? "hot" : options.type;
        let value = options.value == "" || options.value == undefined ? "1" : options.value;
        var title = "热销榜";
        if (channel == "jd") {
            var channelname = "京东";
            var channelename = "jingdong";
            if (type == "hot") {
                var title = value == "1" ? "爆品优选" : "秒杀";
            }
            if (type == "keyword") {
                title = value + "热销榜";
            }
        } else {
            var channelname = "拼多多";
            var channelename = "pinduoduo";
            if (type == "hot") {
                switch (value) {
                  case "1":
                    title = "今日爆款";
                    break;

                  case "2":
                    title = "品牌清仓";
                    break;

                  case "3":
                    title = "秒杀精选";
                    break;
                }
            }
            if (type == "keyword") {
                title = value + "热销榜";
            }
        }
        //页面标题，全屏后可去掉
                wx.setNavigationBarTitle({
            title: channelname + title
        });
        that.getBase();
        that.getMyLeaderInfo();
        //参数：平台名称，页码，数据类型，数据值
                that.getProList(channel, channelename, 1, type, value);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        if (this.data.noshare == false) {
            this.setData({
                noshare: true
            });
        }
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.getProList(that.data.channel, that.data.channelename, 1, that.data.type, that.data.value);
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getProList(that.data.channel, that.data.channelename, that.data.page, that.data.type, that.data.value);
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        let that = this;
        let leaderinfo = that.data.leader ? "&leader=" + that.data.leader : "";
        let commissionNotice = that.data.commissionUser ? "，下单还有返利哦" : "";
        let title = that.data.channelename == "jingdong" ? "京东优选爆品" + commissionNotice : "拼多多精选爆品" + commissionNotice;
        //当团长分享的时候 不显示团长佣金
                if (that.data.iam_leader) {
            that.setData({
                noshare: false
            });
        }
        return {
            title: title,
            path: "/tbkPackage/pages/proList/proList?channel=" + that.data.channel + "&type=" + that.data.type + "&value=" + that.data.value + leaderinfo
        };
    },
    //全局基础配置信息，这个模块暂时只用头部颜色，新版无头的也可能不用了
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                // wx.setNavigationBarColor({
                //   frontColor: "#ffffff",
                //   backgroundColor: res.extra.color_main
                // })
                that.setData({
                    color_main: res.extra.color_main
                });
            }
        });
    },
    //获取团长信息
    getMyLeaderInfo: function() {
        var that = this;
        var user = wx.getStorageSync("user");
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: user.openid
            },
            success: res => {
                //判断哪个团长优先
                //1.我是团长，则优先用自己
                //2.本次分享团长 优先级 大于 历史绑定团长
                if (res.extra.iam_leader) {
                    var leader = res.extra.leader_id;
                } else {
                    var leader = that.data.leader != "" ? that.data.leader : res.extra.leader_id;
                }
                let commissionname = res.extra.iam_leader == 1 ? "返佣" : "返利";
                that.setData({
                    iam_leader: res.extra.iam_leader,
                    leader: leader,
                    commissionname: commissionname
                });
                //获取佣金比例
                                common.request({
                    endpoint: "Taoke/common/brokerage-setting-view",
                    method: "post",
                    data: {},
                    success: res => {
                        //根据身份，判断显示的佣金或返利比例
                        if (that.data.iam_leader) {
                            var commission = res.extra.is_taoke_leader_brokerage == 1 ? res.extra.taoke_leader_brokerage_rate : 0;
                        } else {
                            var commission = res.extra.is_taoke_user_brokerage == 1 ? res.extra.taoke_user_brokerage_rate : 0;
                        }
                        that.setData({
                            commission: commission,
                            commissionUser: res.extra.is_taoke_user_brokerage
                        });
                    }
                });
            }
        });
    },
    //获取商品列表
    getProList: function(channel, channelename, page, type, value) {
        let that = this;
        common.request({
            endpoint: "Taoke/" + channelename + "/get-product-info",
            method: "post",
            data: {
                page: page,
                page_size: 20,
                type: type,
                value: value
            },
            success: function(res) {
                if (res.extra == null) {
                    console.log("走到nodata");
                    that.setData({
                        noData: true
                    });
                }
                if (res.extra.code == 403) {
                    wx.showModal({
                        title: "提示",
                        content: res.extra.message,
                        showCancel: false,
                        success: function() {
                            wx.navigateBack({});
                        }
                    });
                }
                //判断API接口类型，不同接口里的key不一样
                //jd暂时都用一个
                                if (channelename == "jingdong") {
                    var proList = that.data.page == 1 ? res.extra.data : that.data.proList.concat(res.extra.data);
                }
                //pdd爆款
                                if (channelename == "pinduoduo" && type == "hot" && value != 3) {
                    var proList = that.data.page == 1 ? res.extra.goods_basic_detail_response.list : that.data.proList.concat(res.extra.goods_basic_detail_response.list);
                }
                //pdd秒杀
                                if (channelename == "pinduoduo" && type == "hot" && value == 3) {
                    var proList = that.data.page == 1 ? res.extra.top_goods_list_get_response.list : that.data.proList.concat(res.extra.top_goods_list_get_response.list);
                }
                //pdd关键词和品类
                                if (channelename == "pinduoduo" && (type == "keyword" || type == "category")) {
                    var proList = that.data.page == 1 ? res.extra.goods_search_response.goods_list : that.data.proList.concat(res.extra.goods_search_response.goods_list);
                }
                that.setData({
                    channel: channel,
                    channelename: channelename,
                    proList: proList,
                    page: that.data.page + 1,
                    type: type,
                    value: value
                });
            }
        });
    },
    //跳转内页
    goToDetail: function(e) {
        wx.navigateTo({
            url: "/tbkPackage/pages/detail/detail?channel=" + this.data.channelename + "&id=" + e.currentTarget.dataset.id + "&leader=" + this.data.leader
        });
    },
    //监听下拉，显示返回顶部
    onPageScroll: function(e) {
        if (e.scrollTop > 870) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    // 一键回到顶部
    goTop: function() {
        wx.pageScrollTo({
            scrollTop: 0
        });
    }
});